package com.yezong.AidMe.controller;

import com.yezong.AidMe.config.Config;
import com.yezong.AidMe.entity.Order;
import com.yezong.AidMe.entity.Result;
import com.yezong.AidMe.entity.User;
import com.yezong.AidMe.service.AddressLngLatExchange;
import com.yezong.AidMe.service.OrderService;
import com.yezong.AidMe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Logger;

@RequestMapping("order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressLngLatExchange addressLngLatExchange;

    Logger logger = Logger.getLogger("OrderController");
    private Result result;

    @PostMapping("search")
    public Result search(String word){

        logger.info("调用 OrderController 的 search 函数");

        result = new Result(Config.STATUS_SUCCESS,"zjcsxg",orderService.searchByTitleAndDetails(word));

        for (Object item : orderService.searchByTitleAndDetails(word)){
            System.out.println(item.toString());
        }
        return result;
    }

    @PostMapping("recommend")
    public Result recommend(HttpSession session){
        logger.info("调用 OrderController 的 search 函数");

        List l1 = orderService.searchByView();
        List l2 = orderService.searchByPublishTime();
        String[] interests = userService.selectInterest(((User)session.getAttribute("user")).getId());
        List list = null;
        int i = 0;
        while(interests[i] != null){
            list.add(orderService.searchByType(interests[i]));
            i++;
        }
        Collections.shuffle(list);

        if(l1 == null && l2 == null && list == null)
            result = new Result(Config.STATUS_FAILURE, "未找到相关搜索内容", null);
        else{

            result = new Result(Config.STATUS_SUCCESS, "zjcsxz", orderService.unionList(orderService.unionList(l1, l2), list));
        }
        return result;
    }

    @RequestMapping("hottest")
    public Result hottest(String location){
        logger.info("调用 OrderController 的 hottest 函数");

        result = new Result(Config.STATUS_SUCCESS,"zjcsxg",orderService.searchByLocalView(orderService.locate(location)));

        return result;
    }

    @RequestMapping("last")
    public Result last(String location){
        logger.info("调用 OrderController 的 last 函数");

        result = new Result(Config.STATUS_SUCCESS,"zjcsxg",orderService.searchByLocalPublishTime(orderService.locate(location)));

        return result;
    }

    @RequestMapping("closest")
    public Result closest(String location){
        logger.info("调用 OrderController 的 closest 函数");

        if(location == null) result = new Result(Config.STATUS_FAILURE, "我地址呢，老弟", null);

        List<Order> list = orderService.searchAll();

        if(list == null) result = new Result(Config.STATUS_FAILURE, "啥也没有，小老弟", null);

        int i = 0;

        String begin = addressLngLatExchange.getLngLat(location);

        while(list.get(i) != null){
            String end = list.get(i).getPublishlocation();
            Double distance = addressLngLatExchange.toCalDis(begin,end);
            list.get(i).setDistance(distance);
            i++;
        }

        Collections.sort(list,new Comparator<Order>() {
            @Override
            public int compare(Order o1, Order o2){
                if(o1.getDistance()>o2.getDistance()) {
                    return 1;
                }
                else if(o1.getDistance()<o2.getDistance()) {
                    return -1;
                }
                else {
                    return 0;
                }
            }
        });

        result = new Result(Config.STATUS_SUCCESS,"zjcsxg",list);

        return result;
    }

}
